package com.example.readexceldata.service.impl;

import com.alibaba.excel.EasyExcel;
import com.example.readexceldata.dto.WatchImportData;
import com.example.readexceldata.entity.Organization;
import com.example.readexceldata.entity.User;
import com.example.readexceldata.entity.Workstation;
import com.example.readexceldata.mapper.UserMapper;
import com.example.readexceldata.service.IOrganizationService;
import com.example.readexceldata.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.readexceldata.service.IWorkstationService;
import com.example.readexceldata.util.WatchImportDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private IWorkstationService workstationService;
    @Autowired
    private PasswordEncoder passwordEncoder;
   @Override
    public String importWatchesData(MultipartFile multipartFile) {
        WatchImportDataListener dataListener = new WatchImportDataListener(this, null);
        try {
            EasyExcel.read(multipartFile.getInputStream(), WatchImportData.class, dataListener).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<WatchImportData> cachedDataList = dataListener.getCachedDataList();
         this.data(cachedDataList);
        String msg="";
        return msg;
    }
    public void data( List<WatchImportData> data)
    {
        List<WatchImportData> fail=new ArrayList<>();
        int success=0;
        int failNum=0;
        for (WatchImportData datum : data) {
            User user = new User();
            user.setCreatedBy("DKSB");
           user.setCreatedByName("管理员");
           user.setCreatedDate(LocalDateTime.now());
           user.setModifiedDate(LocalDateTime.now());
           user.setModifiedByName("管理员");
           user.setModifiedBy("DKSB");
           user.setEnabled(true);
           user.setLockTime(null);
           user.setPassword(passwordEncoder.encode("Dksb@2332"));
           user.setRole("USER");
           user.setType(datum.getType());
           user.setName(datum.getName());
           user.setPhone(datum.getPhone());
           user.setIdCard(datum.getIdCard());
            List<Workstation> list = workstationService.lambdaQuery()
                    .eq(Workstation::getName, datum.getWangDian())
                    .list();

            if(list.isEmpty())
            {
                fail.add(datum);
                failNum++;
                continue;
            }
            Workstation workstation = list.get(0);
            if(list.size()>0)
            {
                Organization organization = organizationService
                        .lambdaQuery()
                        .eq(Organization::getName, datum.getOrgName())
                        .isNotNull(Organization::getParentCode)
                        .one();
                workstation=workstationService.lambdaQuery()
                        .eq(Workstation::getName, datum.getWangDian())
                        .eq(Workstation::getOrganizationCode, organization.getCode())
                        .one();
            }
            user.setWorkstationId(workstation.getId());
            user.setOrganizationCode(workstation.getOrganizationCode());
            List<User> users = this.lambdaQuery()
                    .eq(User::getPhone, user.getPhone())
                    .eq(User::getName, user.getName())
                    .eq(User::getIdCard, user.getIdCard())
                    .list();
            if(users.isEmpty())
            {
                this.save(user);
                 success++;
            }

        }
        System.out.println("成功："+success+"条");
        System.out.println("失败："+failNum+"条");
        System.out.println("失败数据："+fail.toString());
    }


}
